/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.news.presenter;

import com.yiqi.liebang.entity.bo.PageBo;
import com.yiqi.liebang.entity.bo.MessageBo;
import com.yiqi.liebang.feature.news.contract.MessageContract;
import com.yiqi.liebang.feature.news.dependencies.message.DaggerMessagePresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class MessagePresenter implements MessageContract.Presenter {

    private MessageContract.View mView;

    @Inject

    MessageContract.Model mModel;


    public MessagePresenter(MessageContract.View mView) {
        this.mView = mView;
        DaggerMessagePresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }



    @Override
    public void getMessageList(int pageIndex, int pageSize) {
        mModel.getMessageList(new PageBo(pageIndex, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<MessageBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
//                mView.showLoading(d);
            }

            @Override
            public void onNext(List<MessageBo> couponBoPageData) {
                if (couponBoPageData!=null&&couponBoPageData.size()>0){
                    mView.showListSuccess(couponBoPageData);
                }else {
                    mView.showMessageListEmpty();
                }
//                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
//                mView.dismissLoading();
                mView.showMessageListError(e.getMessage());
            }

            @Override
            public void onComplete() {
//                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getMessageListMore(int pageIndex, int pageSize) {
        mModel.getMessageList(new PageBo(pageIndex, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<MessageBo>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<MessageBo> couponBoPageData) {
                mView.showMessageListMoreSuccess(couponBoPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.showMessageListMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
